package com.wish.group05.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.dto.HouseInfoDto;
import com.wish.group05.common.core.web.page.TableDataInfo;
import com.wish.group05.base.entity.HouseInfo;
import com.wish.group05.base.entity.PremisesInfo;
import com.wish.group05.base.entity.SysConfig;
import com.wish.group05.base.entity.SysDept;
import com.wish.group05.base.mapper.HouseInfoMapper;
import com.wish.group05.base.service.IHouseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.group05.base.service.IPremisesInfoService;
import com.wish.group05.base.service.ISysDeptService;
import com.wish.group05.base.vo.HouseInfoVO;
import com.wish.group05.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author houzehui
 * @since 2022-05-24
 */
@Service
public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo> implements IHouseInfoService {


    @Autowired
    IHouseInfoService iHouseInfoService;
    @Autowired
    IPremisesInfoService iPremisesInfoService;
    @Autowired
    ISysDeptService iDeptService;

    @Override
    public List<HouseInfoVO> selectTbHouseInfoList(HouseInfoDto houseInfoDTO) {
        QueryWrapper<HouseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flg",0);
        //小区名称
        //租房类型
        if(StringUtils.hasText(houseInfoDTO.getRentType())){
            //公寓
            if(houseInfoDTO.getRentType().equals("1")){
                wrapper.eq("type",1);
            }
            //合租
            if(houseInfoDTO.getRentType().equals("3")){
                wrapper.eq("reserve1",1);
            }
            //整租
            wrapper.eq("reserve1",0);
        }
        List<HouseInfo> list = iHouseInfoService.list(wrapper);
        List<HouseInfoVO> houseInfoVOList = new ArrayList<>();
        list.forEach(houseInfo -> {
            HouseInfoVO houseInfoVO = new HouseInfoVO();
            BeanUtil.copyProperties(houseInfo,houseInfoVO);
            houseInfoVOList.add(houseInfoVO);
        });
        //获取所有楼盘id
        List<Integer> premisesIds = houseInfoVOList.stream().map(houseInfoVO -> houseInfoVO.getPremisesId()).collect(Collectors.toList());
        if(premisesIds.size()>0){
            //查询出楼盘信息并赋值
            List<PremisesInfo> premisesInfos = iPremisesInfoService.listByIds(premisesIds);
            //赋值楼盘信息
            for (int i = 0; i < premisesInfos.size() ; i++) {
                HouseInfoVO houseInfoVO = houseInfoVOList.get(i);
                houseInfoVO.setPremisesInfo(premisesInfos.get(i));
                houseInfoVO.setPremiseName(premisesInfos.get(i).getName());
            }
        }
        //获取所有部门id
        List<Integer> deptIds = houseInfoVOList.stream().map(houseInfoVO -> houseInfoVO.getDeptId()).collect(Collectors.toList());
        if(deptIds.size()>0){
            //查询出所有的部门信息并且赋值
            List<SysDept> depts = iDeptService.listByIds(deptIds);
            //赋值部门信息
            for (int i = 0; i < depts.size() ; i++) {
                HouseInfoVO houseInfoVO = houseInfoVOList.get(i);
                houseInfoVO.setDept(depts.get(i));
            }
        }
        houseInfoVOList.forEach(houseInfoVO -> {
            setFcmessage(houseInfoVO);
            //赋值x室x厅
            setShiting(houseInfoVO);
            //设置租房类型
            setrentType(houseInfoVO);
            //赋值房产信息
            houseInfoVO.setHouseName(houseInfoVO.getPremiseName()+houseInfoVO.getShiting());
        });


        return houseInfoVOList;
    }



    public void setShiting(HouseInfoVO houseInfoVO){
        Integer room = houseInfoVO.getRoom();
        Integer hall = houseInfoVO.getHall();
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder fc = stringBuilder.append((room == null ? 0 : room) + "室" + (hall == null ? 0 : hall) + "厅");
        houseInfoVO.setShiting(fc.toString());
    }

    public void setFcmessage(HouseInfoVO houseInfoVO){
        if(houseInfoVO.getPremisesInfo()==null){
            return;
        }
        if(houseInfoVO.getBuildingNo()!=null && houseInfoVO.getUnitNo()!=null && houseInfoVO.getRoomNo()!=null){
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder fc = stringBuilder.append(houseInfoVO.getBuildingNo() + "栋" + houseInfoVO.getUnitNo() + "单元" + houseInfoVO.getRoomNo());
            houseInfoVO.setFcMessage(houseInfoVO.getPremisesInfo().getName()+"("+fc.toString()+")");
            return;
        }
        houseInfoVO.setFcMessage(houseInfoVO.getPremisesInfo().getName());
    }

    public void setrentType(HouseInfoVO houseInfoVO){
        if(houseInfoVO.getType()==1){
            houseInfoVO.setZflx("公寓");
            houseInfoVO.setZflxNumber(3);
            return;
        }
        if(houseInfoVO.getReserve1()==1){
            houseInfoVO.setZflx("合租");
            houseInfoVO.setZflxNumber(1);
            return;
        }
        houseInfoVO.setZflx("整租");
        houseInfoVO.setZflxNumber(2);
    }
}
